/**
 * form表单
 */
seajs.use(["zui/uploadImage",'zui/dialog','zui/utils','zui/ajax'],function(Upload,zDialog,zUtils,zAjax){
	var minPhoto = $("#minPhoto").val();
	$("#zaw-btn-photo").on("click",function(){
		var upLen = $("#imgList1").find(".group_imgupload_view").length;
			if( upLen < minPhoto ){
				App.showToast("您还有资料未上传");
			}else{
				selectPhotoFinish();
			}
	});
	$("#zaw-btn").on("click",function(){
		var upLen = $("#photoRoom").find(".group_imgupload_view").length;
			if( upLen < minPhoto ){
				App.showToast("您还有资料未上传");
			}else{
				selectPhotoFinish();
			}
	});
	//文件上传
	var img;
	//上传文件
	$(document).on("click","#upoladimg1",function(){
		var maxLen = $(this).parents(".za_web_group_imgupload_img").attr("maxLen");
		var upLen = $(this).parents(".za_web_group_imgupload_img").find(".group_imgupload_view").length;
	    var upLoadAddress = $(this).val();	//提交图片路径
	    img = $(this).find("img");
	    $("#tyrePlusImg").click();
	    if( upLen < (maxLen - 1) ){
			$(this).show();
		}else{
			$(this).hide();
		}
	});
	//删除图片
	$(document).on("click",".za_web_img_close",function(){
		var maxLen = $(this).parents(".za_web_group_imgupload_img").attr("maxLen");
		var upLen = $(this).parents(".za_web_group_imgupload_img").find(".group_imgupload_view").length;
		if( upLen <= maxLen ){
			$(".imgupload_room").show();
		}else{
			$(".imgupload_room").hide();
		}
		$(this).parents(".group_imgupload_view").remove();
	});
	//图片上传接口
	function upload(img,form){
		var uploader = 
				new Upload({
					file:img,
					uploadForm:form,
					autoLoad:true,
					url:url,
					readImage:true,
					callback:function(xhr){
						var data;
						if(zUtils.isString(xhr.response)){
							data =JSON.parse(xhr.response);
						}
						else{
							data =xhr.response;
						}
					   if(data.result){
						   if(data.extraInfo){
							   //上传成功
							   uploadSucess(data);
							   $("#uploadForm").replaceWith($("#uploadForm").clone(true));
							   upload("tyrePlusImg","uploadForm");
						   }
					   }
					   else{
						   uploadFail(data);
					   }
					}
				});
		uploader.onLoad = function(event) {  
			var self = this;  
		    if(event.loaded < self.total) {
				self.readBlob(event.loaded);
			} else {
			 	self.loaded = self.total;
			 	url = self.reader.result;
				}
			}
	}
	upload("tyrePlusImg","uploadForm");
	//图片上传成功
	function uploadSucess(data){
		var newRm = '<div class="group_imgupload_view">'+
			    						'<a class="za_web_img_close">X</a>'+
			    						'<img src="'+url+'">'+
			    						// '<input type="hidden" name="picUrl" value="'+data.extraInfo.url+'">'+
			    				'</div>';
		$("#upoladimg1").parents(".imgupload_img_cell").before(newRm);
		App.showToast("上传成功~");
	}
	//图片上传失败
	function uploadFail(data){
		App.showToast("资料上传失败，请重新尝试~");
	}
});
//点击展开上传图片
var endAnimation = function(){
	var x = document.getElementById("moveContain");
	$(x).hide();
    x.removeEventListener("webkitAnimationEnd", endAnimation);
	
};
window.location.hash="#";
window.onload=function(){
	window.onhashchange=function(){
		$('#moveContain').show();
		if(window.location.hash == '#photo'){
			$('#moveContain').removeClass('move_right');
			$('#moveContain').addClass('move_left');
			$('#moveContain').css("height",$("html").height());
		}else{
			$('#moveContain').removeClass('move_left');
			$('#moveContain').addClass('move_right');
			var x = document.getElementById("moveContain");
            x.addEventListener("webkitAnimationEnd", endAnimation);
		};
	};
};
var selectPhoto = function(obj){
	window.location.hash="photo";
};
//提交图片完成，返回
var selectPhotoFinish = function(e){
	window.location.hash="";
	var photoArr = new Array();
	$("#imgList1").find(".group_imgupload_view").each(function(){
		var photoSrc = $(this).find("img").attr("src");
		photoArr.push(photoSrc);
	});
	console.log(photoArr);
	var i = 0;
	var photoList = "";
	for(i;i<photoArr.length;i++){
		var photo = '<div class="imgupload_img_cell imgupload_room"><img src="'+photoArr[i]+'" /></div>';
		photoList += photo;	
	}
	var clear = '<div class="clear"></div>';
	photoList += clear;	
	$("#photoRoom").html(photoList);
};